<template>
  <div id="app">
    <div>
      <span>姓名:</span>
      <input type="text" v-model.trim="user.name" />
    </div>
    <div>
      <span>年龄:</span>
      <input type="number" v-model.number="user.age" />
    </div>
    <div>
      <span>性别:</span>
      <select v-model="user.sex">
        <option value="男">男</option>
        <option value="女">女</option>
      </select>
    </div>
    <div>
      <button @click="addOrEditFn">添加/修改</button>
    </div>
    <div>
      <table
        border="1"
        cellpadding="10"
        cellspacing="0"
        v-show="arr.length > 0"
      >
        <tr>
          <th>序号</th>
          <th>姓名</th>
          <th>年龄</th>
          <th>性别</th>
          <th>操作</th>
        </tr>
        <tr v-for="(obj, index) in arr" :key="index">
          <td>{{ index + 1 }}</td>
          <td>{{ obj.name }}</td>
          <td>{{ obj.age }}</td>
          <td>{{ obj.sex }}</td>
          <td>
            <button @click="delFn(index)">删除</button>
            <button @click="editFn(obj, index)">编辑</button>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      user: {
        // 表单里绑定对象
        name: '',
        age: '',
        sex: ''
      },
      editIndex: -1, // 正在编辑的索引默认打开网页是没有的
      arr: [
        // 数据源
        {
          name: 'Tom',
          age: 19,
          sex: '男'
        },
        {
          name: 'Jone',
          age: 21,
          sex: '女'
        }
      ]
    }
  },
  methods: {
    addOrEditFn () {
      // 添加/修改方法
      if (
        this.user.name.length === 0 ||
        this.user.age === 0 ||
        this.user.sex.length === 0
      ) {
        alert('请输入内容')
        return
      }

      if (this.editIndex === -1) {
        // 新增
        this.arr.push({ ...this.user })
      } else {
        // 编辑
        this.$set(this.arr, this.editIndex, { ...this.user }) // 修改数组里某个值导致v-for更新
      }

      // 清空输入框
      this.user.name = ''
      this.user.age = ''
      this.user.sex = ''
    },
    editFn (theObj, index) {
      // 编辑-点击事件
      this.user = { ...theObj } // 回显
      this.editIndex = index // 正在编辑的索引
    },
    delFn (index) {
      // 删除方法
      this.arr.splice(index, 1)
    }
  }
}
</script>

<style></style>
